//
//  LXRequstDetailViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2020/6/29.
//  Copyright © 2020 zhangbenchao. All rights reserved.
//

#import "LXRequstDetailViewController.h"
#import "LXCommentViewController.h"

#import "LXRequstDetailHeaderView.h"
#import "LXRequstBottomView.h"
#import "LXRequstOrderDetailModel.h"
#import "LXSaveUserInforTool.h"
#import <WebKit/WebKit.h>
#import "LXConfrimOrderViewController.h"
#import "OrderDetailBottomOctionView.h"
#import "SeeWuLiuViewController.h"
#import "BkMoneyBackViewController.h"

#import "BkCancellResionViewController.h"
#import "JZPayViewController.h"
#import "MoreSelectActionSheetView.h"
#import "LXAddressModel.h"
@interface LXRequstDetailViewController ()
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) LXRequstDetailHeaderView *headerView;
@property (nonatomic,strong) LXRequstBottomView *bottomView;
@property (nonatomic,strong) LXRequstOrderDetailModel *model;

@property (nonatomic,strong) OrderDetailBottomOctionView *payView;

@property (nonatomic,strong)  LXAddressModel*addressModel;
@end

@implementation LXRequstDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.payView];
    [_scrollView.mj_header beginRefreshing];
  //  [self userGetcustomersAction];
    [self getmyAddressListRequst];
}

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScreenHeight - Height_NavBar - TSScaleW(50))];
        _scrollView.backgroundColor = kMainBgColor;
        [_scrollView addSubview:self.headerView];
        [_scrollView addSubview:self.bottomView];
        
        [self reloadScrollViewContentSize];
        WS(weakSelf);
        _scrollView.mj_header = [CustomGifHeader headerWithRefreshingBlock:^{
                              [weakSelf requstDetailDataRequst];
                      }];
    }
    return _scrollView;
}
-(OrderDetailBottomOctionView *)payView{
    if (!_payView) {
        _payView = [[OrderDetailBottomOctionView alloc]init];
        _payView.top = TSScreenHeight - Height_NavBar - TSScaleW(50);
        WS(weakSelf);
        _payView.cancellBlock = ^{
               UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"您是否确定取消订单？" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                      //[weakSelf ensureOrderWithOrdernum:model.ordernum];
                    BkCancellResionViewController *vc = [[BkCancellResionViewController alloc]init];
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                    vc.callBackBlock = ^(NSString * _Nonnull resionString) {
                        [weakSelf cancellOrderRequstWithResion:resionString ordernum:weakSelf.model.ordernum];
                    };
                   }];
               [alert addAction:action1];
               UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                 
               }];
               [alert addAction:action2];
               [weakSelf  presentViewController:alert animated:YES completion:nil];
            
        };
        _payView.deleteOrderBlock = ^{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"您是否确定删除订单？" preferredStyle:UIAlertControllerStyleAlert];
             UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                   [weakSelf deleteOrderWithOrdernum:weakSelf.model.ordernum];
                }];
            [alert addAction:action1];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
              
            }];
            [alert addAction:action2];
            [weakSelf  presentViewController:alert animated:YES completion:nil];
            
        };
        _payView.gotoPayBlock = ^{
            JZPayViewController *vc = [[JZPayViewController alloc]init];
            vc.ordernum = weakSelf.model.ordernum;
            vc.moneyAmount = weakSelf.model.goodsprice;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        ///退款
        _payView.moneyBackBlock = ^{
            NSMutableArray *array = [NSMutableArray array];
            NSMutableArray *muArray = [NSMutableArray array];
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dic in weakSelf.model.ordertailList) {
                [array addObject:dic[@"gname"]];
                [muArray addObject:dic[@"odid"]];
                [dataArray addObject:dic];
            };
            [MoreSelectActionSheetView showWithItems: array title:@"选择退款商品" selectedIndexBlock:^(NSArray * _Nonnull selectArray) {
                NSMutableArray *selData = [NSMutableArray array];
                NSMutableArray *odidArray = [NSMutableArray array];
                for (NSString *index in selectArray) {
                    NSString *oid = muArray[index.intValue];
                    NSDictionary *dic = dataArray[index.intValue];
                    [odidArray addObject:oid];
                    [selData addObject:dic];
                }
                BkMoneyBackViewController *vc = [[BkMoneyBackViewController alloc]init];
                vc.ordernum = weakSelf.model.ordernum;
                vc.odidlist = odidArray;
                vc.dataArray = selData;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } cancleBlock:^{
                
            }];
        };
        _payView.ensureHuoBlock = ^{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"您是否确定收货" preferredStyle:UIAlertControllerStyleAlert];
             UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                   [weakSelf orderconfirmWithOrdernum:weakSelf.model.ordernum];
                }];
            [alert addAction:action1];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
              
            }];
            [alert addAction:action2];
            [weakSelf  presentViewController:alert animated:YES completion:nil];
        };
       
        ///去评价
        _payView.gotoCommendBlcok = ^{
            LXCommentViewController *vc = [[LXCommentViewController alloc]init];
            vc.ordernum = weakSelf.model.ordernum;
            vc.dataArray = weakSelf.model.ordertailList;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        ///查看物流
        _payView.seeWuliuBlock = ^{
            SeeWuLiuViewController *vc = [[SeeWuLiuViewController alloc]init];
            vc.logisticsnum = weakSelf.model.logisticsnum;
            vc.logisticsname = weakSelf.model.logisticsname;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }
    return _payView;
}
-(void)reloadScrollViewContentSize{
     _bottomView.top = _headerView.bottom + TSScaleW(10);
    if (TSScreenHeight  < self.bottomView.bottom + TSScaleW(60)) {
        self.scrollView.contentSize = CGSizeMake(0, self.bottomView.bottom + TSScaleW(60));
    }else{
        self.scrollView.contentSize = CGSizeMake(0, TSScreenHeight);
    }
}
-(LXRequstDetailHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[LXRequstDetailHeaderView alloc]init];
        WS(weakSelf);
        _headerView.reloadFrameBlock = ^{
           [weakSelf reloadScrollViewContentSize];
        };
        _headerView.gotoAddressBlock = ^{
            
        };
//        _headerView.gotoAddressBlock = ^{
//            LXAddressListViewController *vc = [[LXAddressListViewController alloc]init];
//            vc.type = AddressListTypeEdting;
//            vc.callBackBlock = ^(LXAddressModel * _Nonnull model) {
//                weakSelf.addressModel = model;
//            };
//            [weakSelf naviPushVc:vc];
//        };
        
    }
    return _headerView;
}
-(LXRequstBottomView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[LXRequstBottomView alloc]init];
        _bottomView.top = _headerView.bottom + TSScaleW(10);
                WS(weakSelf);
        _bottomView.ensureBtnBlock = ^{
//            BkMoneyBackViewController *vc = [[BkMoneyBackViewController alloc]init];
//            [weakSelf.navigationController pushViewController:vc animated:YES];
            [weakSelf userInforRequstGetbuttonsAction];
        };
            }
            return _bottomView;
        }

-(void)userInforRequstGetbuttonsAction{
    
        [NetWorkTools postConJSONWithUrl:@"getbuttons" parameters:@{} success:^(id responseObject) {
            NSString *result = responseObject[@"result"];
            NSString *resultNote = responseObject[@"resultNote"];
            NSString *datastr = responseObject[@"datastr"];
            NSString *url  = kKefuServerUrl;
            if ([result integerValue] == 0) {
                if ( [datastr integerValue] == 0) {
                    url = kKefuServerUrl;
                }else{
                    url = kSelfServerUrl;
                }
                [self gotoWebWithUrl:url title:@"我的客服"];
            }else{
                 [MBProgressHUD showError:resultNote];
            }
                         
        } fail:^(NSError *error) {
            
        }];
}
-(void)deleteOrderWithOrdernum:(NSString *)ordernum{
                //kDeleteskillsUrl
                NSDictionary *pamas = @{@"ordernum":ordernum};
                [NetWorkTools postConJSONWithUrl:kEngineerorderdemanddelete parameters:pamas success:^(id responseObject) {
                       NSString *result = responseObject[@"result"];
                       NSString *resultNote = responseObject[@"resultNote"];
                      
                       if (result.integerValue == 0) {
                           [self.navigationController popViewControllerAnimated:YES];
                       }else{
                           
                       }
                         [MBProgressHUD showError:resultNote];
                   } fail:^(NSError *error) {
                       
                   }];
}
-(void)ensureOrderWithOrdernum:(NSString *)ordernum{

    LXConfrimOrderViewController *vc = [[LXConfrimOrderViewController alloc]init];
    vc.ordernum = ordernum;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)preasntVc:(UIViewController *)vc
{
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
     [self.navigationController presentViewController:vc animated:YES completion:^{
         //
         vc.view.superview.backgroundColor = [UIColor clearColor];
     }];
}
-(void)headerRefresh{
    [self.scrollView.mj_header beginRefreshing];
}
-(void)requstDetailDataRequst{
    //kMyservicestodetailUrl
    NSDictionary *pamas = @{@"ordernum":_ordernum};

          [NetWorkTools postConJSONWithUrl:@"myorderdetail" parameters:pamas success:^(id responseObject) {
              NSString *result = responseObject[@"result"];
              NSString *resultNote = responseObject[@"resultNote"];
              NSDictionary *data = responseObject[@"dataobject"];

              if (result.integerValue == 0) {
                  LXRequstOrderDetailModel *model = [[LXRequstOrderDetailModel alloc]init];
                  [model setValuesForKeysWithDictionary:data];
                  self.model = model;
              }else{
                   [MBProgressHUD showError:resultNote];
              }
         [self.scrollView.mj_header endRefreshing];
          } fail:^(NSError *error) {
         [self.scrollView.mj_header endRefreshing];
          }];
}
-(void)setModel:(LXRequstOrderDetailModel *)model{
    _model = model;
    _headerView.model = _model;
    _bottomView.model = _model;
    _payView.model = _model;
}

-(void)callToServer{
    if ([LXSaveUserInforTool sharedUserTool].telNum.length == 0) {
        return;
    }
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",[LXSaveUserInforTool sharedUserTool].telNum];

    WKWebView * callWebview = [[WKWebView alloc] init];

    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];

    [self.view addSubview:callWebview];
}


#pragma mark ---取消订单
-(void)cancellOrderRequstWithResion:(NSString *)resion ordernum:(NSString *)ordernum
{
    NSDictionary *pamas = @{@"ordernum":ordernum,@"reason":resion};
    [NetWorkTools postConJSONWithUrl:@"ordercancel" parameters:pamas success:^(id responseObject) {
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
          
           if (result.integerValue == 0) {
               [self headerRefresh];
           }else{
               
           }
             [MBProgressHUD showError:resultNote];
       } fail:^(NSError *error) {
           
       }];
}
#pragma mark ---确认收货
-(void)orderconfirmWithOrdernum:(NSString *)ordernum{
    //kDeleteskillsUrl
    NSDictionary *pamas = @{@"ordernum":ordernum};
    [NetWorkTools postConJSONWithUrl:@"orderconfirm" parameters:pamas success:^(id responseObject) {
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
          
           if (result.integerValue == 0) {
               [self headerRefresh];
           }else{
               
           }
             [MBProgressHUD showError:resultNote];
       } fail:^(NSError *error) {
           
       }];
}

#pragma mark -----请求我的地址
-(void)getmyAddressListRequst
{

 NSDictionary * pamas = @{@"nowPage":[NSString stringWithFormat:@"%@",@"1"],@"pageCount":[NSString stringWithFormat:@"%d",kPage_Size]};
    [NetWorkTools postConJSONWithUrl:@"myAddressList" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
        NSArray *array = responseObject[@"dataList"];
       
        if (result.integerValue == 0) {
            for (NSDictionary *dic  in array) {
                NSString *isdefault = dic[@"isdefault"];
                if (isdefault.integerValue == 1) {
                    LXAddressModel *model = [[LXAddressModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    self.addressModel = model;
                    break;
                }
            }
            
        }else{
             [MBProgressHUD showError:resultNote];
        }
         
    } fail:^(NSError *error) {
        
    }];
}
-(void)setAddressModel:(LXAddressModel *)addressModel{
    _addressModel = addressModel;
   // self.headerView.addressModel = _addressModel;
    
}
@end

